package com.hnjd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hnjd.entity.Admins;
import com.hnjd.entity.Types;
import com.hnjd.entity.Users;
import com.hnjd.service.IAdminService;

@Controller
public class AdminController {

	@Autowired
	private IAdminService service;
	
	//登录
	@RequestMapping("/admin/login.do")
	public String login(Admins ad) {
		if(service.login(ad)!=null) {
			return "index.jsp";
		}else {
			return "login.jsp";
		}
	}
	
	/**
	 * 管理员查询
	 */
	@RequestMapping("/admin/adminList.do")
	public String adminList(Model m) {
		List<Admins> adminList = service.adminList();
		m.addAttribute("adminList", adminList);
		return "admin_list.jsp";
	}
	
	/**
	 * 管理员重置密码
	 */
	@RequestMapping("/admin/adminReset.do")
	public String adminReset(Admins admins) {
		service.adminReset(admins);
		return "redirect:/admin/adminList.do";
	}
	
	/**
	 * 管理员新增
	 */
	@RequestMapping("/admin/adminAdd.do")
	public String adminAdd(Admins admins) {
		service.adminAdd(admins);
		return "redirect:/admin/adminList.do";
	}
	
	/**
	 * 管理员删除
	 */
	@RequestMapping("/admin/adminDelete.do")
	public String adminDelete(Integer id) {
		service.adminDelete(id);
		return "redirect:/admin/adminList.do";
	}
	
	
	/**
	 * 订单查询
	 */
	@RequestMapping("/admin/orderList.do")
	public String ordersList(Integer status,Model m){
		if(status==null) {
			m.addAttribute("orderList",service.ordersList());
		}else {
			m.addAttribute("orderList",service.orderListByStatus(status));
		}


		return "/admin/order_list.jsp";
	}

	/**
	 *订单状态修改
	 */
	@RequestMapping("/admin/orderUpdate.do")
	public String orderUpdate(Integer status,Integer id) {
		service.orderUpdate(status, id);
		return "redirect:/admin/orderList.do";
	}
	
	/**
	 * 订单删除
	 */
	@RequestMapping("/admin/orderDelete.do")
	public String orderDelete(Integer id) {

		service.orderDelete(id);
		return "redirect:/admin/orderList.do";
	}
	
	/**
	 * 类目查询
	 */
	@RequestMapping("/admin/typeList.do")
	public String typeList(Model m) {
		List<Types> typeList = service.typeList();
		m.addAttribute("typeList", typeList);
		return "type_list.jsp";
	}
	
	/**
	 * 类目添加
	 */
	@RequestMapping("/admin/typeAdd.do")
	public String typeAdd(Types types) {
		service.typeAdd(types);
		return "redirect:/admin/typeList.do";
	}
	
	/**
	 * 类目id查询
	 */
	@RequestMapping("/admin/typeEditById.do")
	public String typeEditById(Integer id,Model m) {
		Types types = service.typeEditById(id);
		m.addAttribute("types", types);
		return "type_edit.jsp";
	}
	
	/**
	 * 类目修改
	 */
	@RequestMapping("/admin/typeUpdate.do")
	public String typeUpdate(Types types) {
		service.typeUpdate(types);
		return "redirect:/admin/typeList.do";
	}
	
	/**
	 * 类目删除
	 */
	@RequestMapping("/admin/typeDelete.do")
	public String typeDelete(Integer id) {
		service.typeDelete(id);
		return "redirect:/admin/typeList.do";
	}
	
	/**
	 * 客户查询
	 */
	@RequestMapping("/admin/userList.do")
	public String userList(Model m) {
		List<Users> userList = service.userList();
		m.addAttribute("userList", userList);
		return "user_list.jsp";
	}
	
	/**
	 * 客户添加
	 */
	@RequestMapping("/admin/userAdd.do")
	public String userAdd(Users users) {
		service.userAdd(users);
		return "redirect:/admin/userList.do";
	}
	
	/**
	 * 客户查询id
	 */
	@RequestMapping("/admin/userEdit.do")
	public String userEdit(Integer id,Model m) {
		Users users = service.userEdit(id);
		m.addAttribute("users", users);
		return "user_edit.jsp";
	}
	
	
	/**
	 * 客户修改
	 */
	@RequestMapping("/admin/userUpdate.do")
	public String userUpdate(Users users) {
		service.userUpdate(users);
		return "redirect:/admin/userList.do";
	}
	
	
	/**
	 * 客户删除
	 */
	@RequestMapping("/admin/userDelete.do")
	public String userDelete(Integer id) {
		service.userDelete(id);
		return "redirect:/admin/userList.do";
	}
	
	/**
	 * 客户重置密码
	 */
	@RequestMapping("/admin/userReset.do")
	public String userReset(Users users) {
		service.userReset(users);
		return "redirect:/admin/userList.do";
	}
}
